package com.msxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msxt.dto.KefangleixingEntity;
import com.msxt.mapper.KefangleixingDao;
import com.msxt.result.Query;
import com.msxt.service.KefangleixingService;
import com.msxt.utils.PageUtils;
import com.msxt.view.KefangleixingView;
import com.msxt.vo.KefangleixingVO;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 客房类型服务实现类
 */
@Service("kefangleixingService")
public class KefangleixingServiceImpl extends ServiceImpl<KefangleixingDao, KefangleixingEntity>
		implements KefangleixingService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Page<KefangleixingEntity> page = this.page(
				new Query<KefangleixingEntity>(params).getPage(),
				new QueryWrapper<>()
		);
		return new PageUtils(page);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params, QueryWrapper<KefangleixingEntity> wrapper) {
		Page<KefangleixingView> page = new Query<KefangleixingView>(params).getPage();

		// === 添加空条件检查 ===
		if (wrapper.isEmptyOfNormal()) {
			wrapper.isNotNull("id"); // 添加永真条件
		}

		// === 手动处理分页计数 ===
		long total = baseMapper.selectCount(wrapper); // 使用MyBatis-Plus的selectCount
		page.setTotal(total);

		if (total > 0) {
			page.setRecords(baseMapper.listView(page, wrapper));
		} else {
			page.setRecords(Collections.emptyList());
		}

		return new PageUtils(page);
	}

	@Override
	public List<KefangleixingVO> listVO(QueryWrapper<KefangleixingEntity> wrapper) {
		return baseMapper.listVO(wrapper);
	}

	@Override
	public KefangleixingVO selectVO(QueryWrapper<KefangleixingEntity> wrapper) {
		return baseMapper.selectVO(wrapper);
	}

	@Override
	public List<KefangleixingView> listView(QueryWrapper<KefangleixingEntity> wrapper) {
		return baseMapper.listView(wrapper);
	}

	@Override
	public KefangleixingView selectView(QueryWrapper<KefangleixingEntity> wrapper) {
		return baseMapper.selectView(wrapper);
	}
}